//package com.sunlands.deskmate.sales.sender.http;
//
//import com.alibaba.fastjson.JSON;
//import com.sunlands.deskmate.sales.channel.ChannelManager;
//import com.sunlands.deskmate.sales.message.outbound.OutboundMessage;
//import com.sunlands.deskmate.sales.sender.RetrySender;
//import lombok.extern.slf4j.Slf4j;
//
//import java.util.List;
//import java.util.concurrent.*;
//
///**
// * @Author: anjunling@sunlands.com
// * @Description:
// * @Date: Create in 12:48 2018/6/14
// * @Modified By:
// */
//@Slf4j
//public class RetryHttpMessageSender extends AbstractHttpMessageSender implements RetrySender<List<OutboundMessage>, Integer> {
//
//    private static volatile Object lock = null;
//
//    private static final BlockingQueue<RetryHttpMessageSender> queue = new LinkedBlockingQueue<>();
//
//    private static final ExecutorService pool = Executors.newFixedThreadPool(Math.max(100, Runtime.getRuntime().availableProcessors()*2));
//
//    public RetryHttpMessageSender() {
//    }
//
//    public RetryHttpMessageSender(ChannelInfo channelInfo, String url, List<OutboundMessage> messages) {
//        this.channelInfo = channelInfo;
//        this.url = url;
//        this.messages = messages;
//    }
//
//    @Override
//    public void send() {
//        super.send();
//        if (!sendResult) {
//            release();
//
//            if (log.isDebugEnabled()) {
//                log.debug("http retry send fail {}", channelInfo);
//            }
//        }
//    }
//
//    @Override
//    public void retry() {
//        run();
//        boolean suc = queue.offer(this);
//        if (!suc) {
//            log.debug("http retry suc={}", suc);
//        }
//    }
//
//    @Override
//    public Integer strategy() {
//        return 2;
//    }
//
//    /**
//     * 启动重试器
//     */
//    private void run() {
//        if (lock == null) {
//            synchronized (RetryHttpMessageSender.class) {
//                if (lock == null) {
//                    lock = new Object();
//                    Thread thread = new Thread(new Worker());
//                    thread.start();
//                }
//            }
//        }
//    }
//
//    private static class Worker implements Runnable {
//
//        @Override
//        public void run() {
//            boolean isSuccess = false;
//            while (!isSuccess || Thread.currentThread().isInterrupted()) {
//                RetryHttpMessageSender retryHttpMessageSender = null;
//                try {
//                    retryHttpMessageSender = take();
//                    if (retryHttpMessageSender == null) {
//                            log.warn("http take is empty");
//                            continue;
//                    }
//                    isSuccess = retryHttpMessageSender.sendResult;
//
//                    CompletableFuture.completedFuture(retryHttpMessageSender).thenAcceptAsync(sender -> sender.send(), pool);
//                } catch(InterruptedException ie) {
//                    log.error("retry http interrupted exception");
//                    Thread.currentThread().interrupt();
//                } catch (Exception e) {
//                    if (retryHttpMessageSender != null) {
//                        log.error("http send fail channelInfo {} messages {}", retryHttpMessageSender.channelInfo, JSON.toJSONString(retryHttpMessageSender.messages));
//                    } else {
//                        log.error("http send fail", e);
//                    }
//                }
//            }
//        }
//
//        private RetryHttpMessageSender take() throws InterruptedException {
//            RetryHttpMessageSender sender;
//            try {
//                sender = queue.take();
//            } catch (InterruptedException ie) {
//                log.warn("take retry");
//                throw ie;
//            }
//            return sender;
//        }
//    }
//
//}
